
var db = require( '../db/db' );

const TABLE_NAME = 'core.user_consume';

let create = async function( userId, plan, storage, apiCall, georeference, startAt, endAt ){

	let sql = `INSERT INTO ${TABLE_NAME} (user_id, plan, storage_consume, api_call_consume, georeference_consume, start_at, end_at, created_at, updated_at) 
								VALUES ( $1, $2, $3, $4, $5, $6, $7, now(), now() )`;
	let result = db.runSql(sql, [ userId, plan, storage, apiCall, georeference, startAt, endAt ]);

	return result != false;

};

let getByUserId = async function( userId ){

	let sql = `SELECT plan, storage_consume::INTEGER, georeference_consume::INTEGER FROM ${TABLE_NAME} WHERE user_id=$1 LIMIT 1`;

	let result = await db.runSql(sql, [ userId ]);

	if(result && result.rows && result.rows[0] ){

		return result.rows[0];

	}

	return null;

};

module.exports = {

	create,
	getByUserId

}